const db = require('../config/db');

// 获取所有集装箱信息
async function getAllContainers(req, res) {
  try {
    const [rows] = await db.execute('SELECT * FROM information');
    res.json(rows);
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: '获取集装箱数据失败' });
  }
}

// 根据ID获取集装箱信息
async function getContainerById(req, res) {
  try {
    const { id } = req.params;
    const [rows] = await db.execute('SELECT * FROM information WHERE id = ?', [id]);
    if (rows.length === 0) {
      return res.status(404).json({ error: '集装箱信息不存在' });
    }
    res.json(rows[0]);
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: '获取集装箱数据失败' });
  }
}

// 创建新集装箱信息
async function createContainer(req, res) {
  try {
    const { terminal_name, bill_of_lading, container_number, container_size_type, container_type, 
            seal_number, gross_weight, tare_weight, terminal_release, current_status } = req.body;
    const [result] = await db.execute(
      'INSERT INTO information (terminal_name, bill_of_lading, container_number, container_size_type, container_type, seal_number, gross_weight, tare_weight, terminal_release, current_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
      [terminal_name, bill_of_lading, container_number, container_size_type, container_type, 
       seal_number, gross_weight, tare_weight, terminal_release, current_status]
    );
    res.status(201).json({ id: result.insertId, message: '集装箱信息创建成功' });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: '创建集装箱信息失败' });
  }
}

// 更新集装箱信息
async function updateContainer(req, res) {
  try {
    const { id } = req.params;
    const { terminal_name, bill_of_lading, container_number, container_size_type, container_type, 
            seal_number, gross_weight, tare_weight, terminal_release, current_status } = req.body;
    const [result] = await db.execute(
      'UPDATE information SET terminal_name = ?, bill_of_lading = ?, container_number = ?, container_size_type = ?, container_type = ?, seal_number = ?, gross_weight = ?, tare_weight = ?, terminal_release = ?, current_status = ? WHERE id = ?',
      [terminal_name, bill_of_lading, container_number, container_size_type, container_type, 
       seal_number, gross_weight, tare_weight, terminal_release, current_status, id]
    );
    if (result.affectedRows === 0) {
      return res.status(404).json({ error: '集装箱信息不存在' });
    }
    res.json({ message: '集装箱信息更新成功' });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: '更新集装箱信息失败' });
  }
}

// 删除集装箱信息
async function deleteContainer(req, res) {
  try {
    const { id } = req.params;
    const [result] = await db.execute('DELETE FROM information WHERE id = ?', [id]);
    if (result.affectedRows === 0) {
      return res.status(404).json({ error: '集装箱信息不存在' });
    }
    res.json({ message: '集装箱信息删除成功' });
  } catch (err) {
    console.error(err);
    res.status(500).json({ error: '删除集装箱信息失败' });
  }
}

module.exports = {
  getAllContainers,
  getContainerById,
  createContainer,
  updateContainer,
  deleteContainer
};